package com.example.androidlastwork.register;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androidlastwork.R;

import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class RegisterActivity extends AppCompatActivity {

    private EditText edusername;
    private EditText edpwd1;
    private EditText edpwd2;
    private Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //绑定控件
        getViews();



        //点击注册
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入框中的内容
                String username = edusername.getText().toString();
                String pwd1 = edpwd1.getText().toString();
                String pwd2 = edpwd2.getText().toString();
                //1.判断是否为空
                if (username.equals("") || pwd1.equals("") || pwd2.equals("")){
                    //弹窗警告  用户名或密码不能为空
                    //弹窗用户名或密码错误
                    new AlertDialog.Builder(RegisterActivity.this)
                            .setTitle("注册失败")
                            .setMessage("用户名或密码不能为空")
                            .show();
                }else if(!pwd1.equals(pwd2)){
                    new AlertDialog.Builder(RegisterActivity.this)
                            .setTitle("注册失败")
                            .setMessage("两次输入密码不一致")
                            .show();
                }else{
                    //向服务器传数据
                    sendToServer(username,pwd1);
                }




            }
        });




    }

    private void sendToServer(String username, String pwd1)  {

        String urlPath = "http://10.0.2.2:8080/bigwork/register?username="+username+"&password="+pwd1;

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(urlPath);
                    //开始发送
                    url.openStream();
                    //接收服务器传来的数据
                    URLConnection urlConnection = url.openConnection();
                    InputStream inputStream = urlConnection.getInputStream();
                    //创建字符流读取数据
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
                    String str = bufferedReader.readLine();
                    //服务器发来的数据
                    System.out.println(str);



                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


//                //通过指定URL向服务器发送数据
//                HttpClient httpClient = new DefaultHttpClient();
//                //地址
//                //创建代表请求对象，参数是访问服务器地址
//                HttpGet httpGet = new HttpGet(urlPath);
//                //执行请求
//




//                    URL url = new URL(urlPath+"?username="+username+"&password="+pwd1);
//                    URL url = new URL(urlPath);
//                    System.out.println("username:"+username+",password:"+pwd1);
//                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//
//                    //设置编码方式
//                    connection.setRequestMethod("POST");
//                    //获取网路输出流
//                    OutputStream outputStream = connection.getOutputStream();
//                    //获取待发送的字符串
//                    String str = (username+":"+pwd1).trim();
//                    outputStream.write(str.getBytes());
//                    //获取网络输入流，保证客户端与服务器建立连接
//                    connection.getInputStream();
//                    outputStream.close();

                    //                    url.openStream();

//                    //接收服务器传来的数据
//                    URLConnection urlConnection = url.openConnection();
//                    InputStream inputStream = urlConnection.getInputStream();
//                    //创建字符流读取数据
//                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
//                    String str = bufferedReader.readLine();
//
////                    向服务器发送数据成功
//                    System.out.println(str);
//
//                    bufferedReader.close();
//                    inputStream.close();



            }
        }).start();


    }


    //绑定控件
    private void getViews() {
        edusername = findViewById(R.id.user_name);
        edpwd1 = findViewById(R.id.user_password1);
        edpwd2 = findViewById(R.id.user_password2);
        btnRegister = findViewById(R.id.register);

    }
}